package com.recource.vo;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Function;

/**
 * @author xjhStart
 * @create 2022-11-26 下午5:36
 */
public class PageVO {

    public static <T, R> Page<R> makeVO(Page<T> page, Function<T, R> consumer) {
        List<R> rList = new ArrayList<>();
        List<T> records = page.getRecords();
        Iterator<T> iterator = records.iterator();
        while (iterator.hasNext()) {
            R apply = consumer.apply(iterator.next());
            rList.add(apply);
        }
        Page<R> returnPage = new Page<>();
        returnPage.setCurrent(page.getCurrent());
        returnPage.setSize(page.getSize());
        returnPage.setRecords(rList);
        returnPage.setPages(page.getPages());
        returnPage.setCountId(page.countId());
        returnPage.setMaxLimit(page.maxLimit());
        returnPage.setOptimizeCountSql(page.optimizeCountSql());
        returnPage.setOrders(page.orders());
        returnPage.setSearchCount(page.searchCount());
        returnPage.setTotal(page.getTotal());
        return returnPage;
    }
}
